/*
 * Copyright (c) 2018.
 * http://www.ulabcare.com
 */

package com.cigs.wcs00a.common.base;


/**
 * REST API 错误码
 *
 * @author jiff
 * @date 2018/11/1
 * @since 1.0
 */
public enum ApiErrorCode implements IErrorCode {

    /**
     * 成功
     */
    SUCCESSFUL(CODE_SUCCESSFUL, "成功"),
    /**
     * 失败
     */
    FAILED(CODE_FAILED, "失败"),
    /**
     * 无效的请求参数
     */
    //INVALID_PARAMETER(CODE_INVALID_PARAMETER, "无效的请求参数"),
    INVALID_PARAMETER(CODE_INVALID_PARAMETER, "Invalid request parameter"),
    /**
     * 数据未授权
     */
    FORBIDDEN_DATA(CODE_FORBIDDEN_DATA, "数据未授权"),
    /**
     * 用户已存在
     */
    USER_EXISTENT(CODE_USER_EXISTENT, "用户已存在"),
    /**
     * 用户不存在
     */
    //USER_NON_EXISTENT(CODE_USER_NON_EXISTENT, "用户不存在"),
    USER_NON_EXISTENT(CODE_USER_NON_EXISTENT, "User does not exist"),
    /**
     * 用户已停用
     */
    USER_DISABLED(CODE_FAILED, "用户已停用"),
    /**
     * 用户不存在或密码错误
     */
    USER_NON_EXISTENT_OR_INVALID_PASSWORD(CODE_FAILED, "The user does not exist or the password is wrong"),
    //USER_NON_EXISTENT_OR_INVALID_PASSWORD(CODE_FAILED, "用户不存在或密码错误"),
    /**
     * 密码错误
     */
    //INVALID_PASSWORD(CODE_FAILED, "密码错误"),
    INVALID_PASSWORD(CODE_FAILED, "Wrong password"),

    /**
     * 电话号码不能为空
     */
    MOBILE_IS_EMPTY(CODE_FAILED, "手机号码不能为空"),

    /**
     * 用户未登录
     */
    UNAUTHORIZED(CODE_UNAUTHORIZED, "用户未登录"),
    /**
     * 用户未授权
     */
    FORBIDDEN(CODE_FORBIDDEN, "用户未授权");

    private final int code;
    private final String msg;

    ApiErrorCode(final int code, final String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static ApiErrorCode fromCode(int code) {
        ApiErrorCode[] apiErrorCodes = ApiErrorCode.values();
        for (ApiErrorCode apiErrorCode : apiErrorCodes) {
            if (apiErrorCode.getCode() == code) {
                return apiErrorCode;
            }
        }
        return SUCCESSFUL;
    }

    @Override
    public int getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        return msg;
    }

    @Override
    public String toString() {
        return String.format(" ErrorCode:{code=%s, msg=%s} ", code, msg);
    }
}
